import hatch_interface

options = {}    # Only need to set non-default options.  See hatch_interface.optionDefaults for valid keys.
options["delimiter"] = '_'   # XXX Use a flat interface, does not have all the features of the nested interface.
options["xmlFile"] = 's3board_hatch.xml'
options["bus"] = 'dcom_apb'
options["historyFile"] = __file__
options["debug"] = False
#options["port"] = optionDefaults["port"]
if False:
    options["debug"] = True
    options["bus"] = 'dummy'


hi = hatch_interface.setup_interface(options)   # XXX Create the interface object.


HIT_ENTER = "Hit enter to continue."


# Demonstrate node attributes
if True:
    print hi.nodesMap['s3board_led']['name']    # 'name' attribute.
    print hi.nodesMap['s3board_led']['childNodes']  # 'childNodes' attribute.
    #print hi.nodesMap['s3board_led']['parent']  # 'parent' attribute. TODO - add this.
    print hi.nodesMap['s3board_led']    # 'properties' attribute.
    raw_input(HIT_ENTER)

# Demonstrate put() & get() functions.
if True:
    hi.put(0x22)    # Send write opcode...
    hi.put(0x22, 0x00,0x00,0x00,0x08, 0x00,0x00,0x00,0x1C)  # (Write...) 1C to address 8.
    hi.put([0x21, 0x00,0x00,0x00,0x08])   # Read value in address 8 (brackets optional.)
    print hex(hi.get(4))    # Get and print the returned value from the interface.
    raw_input(HIT_ENTER)
    hi.put(*[0x22, 0x00,0x00,0x00,0x08, 0x00,0x00,0x00,0x00])  # Write 0x00 to address 8 (*args list optional.)

# Demonstrate read() & write() functions.
if True:
    print hex(hi.s3board_led) # Read 'led' register.
    hi.s3board_led = 0xFF   # Write 0xFF to 'led' register.
    # TODO hi.read(), hi.write()
    raw_input(HIT_ENTER)

# Demonstrate recursive reads & writes.
if True:
    print [hex(value) for value in hi.s3board_cPack]    # TODO this is always depth first.
    hi.s3board_cPack = 0xC  # Single value 
    print [hex(value) for value in hi.s3board_cPack]
    hi.s3board_cPack = 0xA,0xB,0xC,0xD,0xE,0xF  # Multiple values (extra values are ignored.)
    hi.s3board_cPack = [0xD,0xC,0xB]    # Brackets optional (values are always written to child Nodes until we run out of values.)
    raw_input(HIT_ENTER)


